package huoye.shops.huoye.operation.activity;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeideology.android.previewimage.PrevieImage;
import com.codeideology.android.previewimage.PrevieImageActivity;
import com.codeideology.android.utils.BarUtils;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.BaseTakePhotoActivity;
import huoye.shops.base.UserManager;
import huoye.shops.common.model.OssImageData;
import huoye.shops.huoye.operation.model.AddImage;
import huoye.shops.huoye.operation.presenter.EditAddShopPresenter;
import huoye.shops.huoye.operation.view.IEditAddShopView;
import huoye.shops.huoye.operation.widget.SelectImageDialog;
import huoye.shops.widget.AmountEditText;

public class EditAddShopActivity extends BaseTakePhotoActivity implements IEditAddShopView{


    @BindView(R.id.edit_add_shop_et_prize_name)
    EditText etPrizeName;
    @BindView(R.id.edit_add_shop_et_prize_price)
    AmountEditText etPrizePrice;
    @BindView(R.id.edit_add_shop_et_prize_stock)
    EditText etPrizeStock;

    @BindView(R.id.edit_add_shop_iv_del_pic_logo)
    ImageView ivDelPicLogo;

    @BindView(R.id.edit_add_shop_iv_add_pic_logo)
    ImageView ivAddPicLogo;

    @OnClick(R.id.edit_add_shop_iv_add_pic_logo)
    void onAddPicLogoClick(){
        takePhoto();
    }

    @OnClick(R.id.edit_add_shop_iv_del_pic_logo)
    void onDelPicLogoClick(){
        mEditAddShopPresenter.deleteOssImg(imgPath);
    }



    private EditAddShopPresenter mEditAddShopPresenter;

    @OnClick(R.id.edit_add_shop_btn_cancel)
    void onCancelClick(){
        finish();
    }

    private String imgPath = "";

    @OnClick(R.id.edit_add_shop_btn_save_message)
    void onSaveMessageClick(){
        String etName = etPrizeName.getText().toString();
        String etPrice = etPrizePrice.getText().toString();
        String etStock = etPrizeStock.getText().toString();

       mEditAddShopPresenter.savePrize(etName,etPrice,etStock,imgPath,1);


    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_edit_add_shop;
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {
        setTitle("编辑");

        mEditAddShopPresenter = new EditAddShopPresenter();
        mEditAddShopPresenter.addTaskListener(this);
        mEditAddShopPresenter.onStart();
        ivDelPicLogo.setVisibility(View.GONE);
    }



    @Override
    public void takeSuccess(TResult result) {
        if (result != null) {
            TImage image = result.getImage();
            imgPath = image.getCompressPath();
            String userShopId = UserManager.getInstance().getUserShopId();
            mEditAddShopPresenter.uploadOssImg(userShopId,"shop",imgPath);

            Glide.with(this)
                    .load(new File(imgPath))
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .placeholder(R.mipmap.ic_wait_logo)
                    .error(R.mipmap.ic_error)
                    .into(ivAddPicLogo);
            ivDelPicLogo.setVisibility(View.VISIBLE);
        }
    }



    @Override
    public void setUpload(OssImageData ossImageData) {
        imgPath = ossImageData.getFilePath();
    }

    @Override
    public void delUpload() {
        ivAddPicLogo.setImageResource(R.mipmap.ic_add_image);
        ivDelPicLogo.setVisibility(View.GONE);
    }
















}
